package api

import (
	"fmt"
	"time"
)

var (
	datetime string
)

func init() {
	fmt.Println("api模块初始化成功……")
}

func Welcome() {
	now := time.Now()
	year := now.Year()
	month := int(now.Month())
	day := now.Day()
	hour := now.Hour()
	minute := now.Minute()
	second := now.Second()
	datetime = fmt.Sprintf("%d-%d-%d %d:%d:%d", year, month, day, hour, minute, second)
	fmt.Printf("现在的时间是01：%v\n", datetime)
	datetime = now.Format("2006/01/02 15:04:05")
	fmt.Printf("现在的时间是02：%v\n", datetime)
}

func Access() {
	fmt.Printf("陈文小超欢迎你来到今日的golang语言学习，现在的时间是：%v\n", datetime)
	fmt.Println("---------------------")

	// 切片练习
	slice()
}

// 切片练习 slice
func slice() {
	var slice1 []int = make([]int, 5, 10)
	slice1[0] = 1
	slice1[1] = 2
	slice1[2] = 3
	slice1[3] = 4
	slice1[4] = 5
	fmt.Println(slice1)
	// 追加切片
	slice1 = append(slice1, 6, 7, 8)
	fmt.Println(slice1)
	fmt.Println("-------------------")
	// copy切片
	var slice2 []int = []int{1, 2, 3, 4, 5}
	var slice3 []int = make([]int, 2)
	copy(slice3, slice2)
	fmt.Println(slice2)
	fmt.Println(slice3)
	fmt.Println("-------------------")

	str := "venki.chen"
	slice4 := str[6:]
	fmt.Println(slice4)

	// 斐波那契
	fmt.Println(fbnq(100))
	// 排序和查找
	/*var arr1 = [5]int{24, 69, 80, 57, 13}
	sort(&arr1)
	var arr2 = [5]string{"白眉鹰王", "金毛狮王", "紫衫龙王", "青翼蝠王"}
	var name string
	fmt.Println("请输入你要查找的四大法王的名字：")
	fmt.Scanln(&name)
	res := find(&arr2, *(&name))
	fmt.Println(res)*/

	// 二分查找
	/*var arr = [7]int{1, 2, 3, 4, 5, 6, 7}
	var target int
	fmt.Println("请输入您要查找的数据：")
	fmt.Scanln(&target)
	half(&arr, 0, len(arr)-1, target)*/
	fmt.Println("-------------")
	// 二维数组
	// twoArr()

	// 二维数组应用案例
	var scores [3][5]float64
	for i := 0; i < len(scores); i++ {
		for j := 0; j < len(scores[i]); j++ {
			fmt.Printf("请输入第%d个班，第%d个学生的成绩：", i+1, j+1)
			fmt.Scanln(&scores[i][j])
		}
	}
	total, number, avg, arr := applyArr(&scores)
	fmt.Printf("学生总数为：%v\n\n", number)
	fmt.Printf("学生总分为：%v\n\n", total)
	fmt.Printf("整个年级的平均分是：%v\n\n", avg)
	fmt.Printf("第%d个班级的平均分是：%v\n\n", 1, arr[0])
	fmt.Printf("第%d个班级的平均分是：%v\n\n", 2, arr[1])
	fmt.Printf("第%d个班级的平均分是：%v\n\n", 3, arr[2])
}

// 二维数组应用
func applyArr(scores *[3][5]float64) (float64, int64, float64, [3]float64) {
	var total float64 = 0.00
	var number int64 = 0
	var arr [3]float64
	for i := 0; i < len(scores); i++ {
		sum := 0.00
		for j := 0; j < len(scores[i]); j++ {
			number++
			sum += scores[i][j]
		}
		arr[i] = sum / float64(len(scores[i]))
		total += sum
	}

	return total, number, total / float64(number), arr
}

func twoArr() {
	// 声明定义二维数组
	var arr [4][6]int
	// 赋值
	arr[1][2] = 1
	arr[2][1] = 2
	arr[2][3] = 3
	fmt.Println(arr)
	// 遍历二维数组
	for i := 0; i < len(arr); i++ {
		for j := 0; j < len(arr[i]); j++ {
			fmt.Printf("%v ", arr[i][j])
		}
		fmt.Println()
	}
}

// 二分查找
func half(arr *[7]int, left int, right int, target int) {
	middle := (left + right) / 2

	if left > right {
		fmt.Println("没有找到目标数")
	}

	if target > (*arr)[middle] {
		half(arr, middle, right, target)
	} else if target < (*arr)[middle] {
		half(arr, left, middle, target)
	} else {
		fmt.Printf("恭喜你找到了目标查找数,其位置是：%v\n", middle)
	}
}

// 查找
func find(arr2 *[5]string, name string) string {
	for i := 0; i < len(*arr2); i++ {
		if name == (*arr2)[i] {
			fmt.Printf("找到了%v,下标是：%v\n", (*arr2)[i], i)
			break
		} else if i == len(*arr2)-1 {
			fmt.Printf("没有找到目标%v\n", name)
		}
	}

	return name
}

// 冒泡排序
func sort(arr1 *[5]int) {
	for i := 0; i < len(*arr1)-1; i++ {
		for j := 0; j < len(*arr1)-1; j++ {
			if (*arr1)[j] < (*arr1)[j+1] {
				(*arr1)[j] = (*arr1)[j] + (*arr1)[j+1]
				(*arr1)[j+1] = (*arr1)[j] - (*arr1)[j+1]
				(*arr1)[j] = (*arr1)[j] - (*arr1)[j+1]
			}
		}
	}
	fmt.Println(*arr1)
}

func fbnq(n int) []uint64 {
	fbnqSlice := make([]uint64, n)
	fbnqSlice[0] = 1
	fbnqSlice[1] = 1

	for i := 2; i < n; i++ {
		fbnqSlice[i] = fbnqSlice[i-1] + fbnqSlice[i-2]
	}

	return fbnqSlice
}
